package com.example.demo.models.base;

import gaarason.database.annotation.Column;
import gaarason.database.annotation.Primary;
import gaarason.database.contract.connection.GaarasonDataSource;
import gaarason.database.contract.eloquent.Builder;
import gaarason.database.contract.eloquent.Record;
import gaarason.database.contract.support.FieldFill;
import gaarason.database.eloquent.Model;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;

@Data
@Accessors(chain = true)
public abstract class BaseEntity extends com.example.demo.services.system.models.base.BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * auto generator start
     **/

    @Column(name = "deleted_at", nullable = true, selectable = false)
    private LocalDateTime deletedAt;


    /**
     * auto generator end
     **/
    public abstract static class BaseModel<T extends BaseEntity, K> extends com.example.demo.services.system.models.base.BaseEntity.BaseModel<T, K> {

        /**
         * 是否启用软删除
         */
        @Override
        protected boolean softDeleting() {
            return false;
        }

        /**
         * 删除(软/硬删除)
         *
         * @param builder 查询构造器
         * @return 删除的行数
         */
        @Override
        public int delete(Builder<T, K> builder) {
            return softDeleting() ? softDelete(builder) : builder.forceDelete();
        }

        /**
         * 恢复软删除
         *
         * @param builder 查询构造器
         * @return 删除的行数
         */
        @Override
        public int restore(Builder<T, K> builder) {
            return softDeleteRestore(builder);
        }

        /**
         * 软删除查询作用域(反)
         *
         * @param builder 查询构造器
         */
        @Override
        protected void scopeSoftDeleteOnlyTrashed(Builder<T, K> builder) {
            builder.whereNotNull("deleted_at");
        }

        /**
         * 软删除查询作用域(反)
         *
         * @param builder 查询构造器
         */
        protected void scopeSoftDeleteWithTrashed(Builder<T, K> builder) {

        }

        /**
         * 软删除查询作用域
         *
         * @param builder 查询构造器
         */
        @Override
        protected void scopeSoftDelete(Builder<T, K> builder) {
            builder.whereNull("deleted_at");
        }


        /**
         * 软删除实现
         *
         * @param builder 查询构造器
         * @return 删除的行数
         */
        @Override
        protected int softDelete(Builder<T, K> builder) {
            return builder.data("deleted_at", LocalDateTime.now()).update();
        }

        /**
         * 恢复软删除实现
         *
         * @param builder 查询构造器
         * @return 恢复的行数
         */
        @Override
        protected int softDeleteRestore(Builder<T, K> builder) {
            return builder.data("deleted_at", null).update();
        }
    }
}